<?php
/*
 *      Redistribution and use in source and binary forms, with or without
 *      modification, are permitted provided that the following conditions are
 *      met:
 *      
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with the
 *        distribution.
 *      * Neither the name of the  nor the names of its
 *        contributors may be used to endorse or promote products derived from
 *        this software without specific prior written permission.
 *      
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

class ti_template
{
	function __construct()
	{
		global $ti;
		$this->type = null;

		
		if(file_exists($ti->paths['server'].'/'.$ti->config['template'].'/'.$ti->config['template.layout'].'.html'))
		{
			$this->type = 'html';
			$this->html = file_get_contents($ti->paths['server'].'/'.$ti->config['template'].'/'.$ti->config['template.layout'].'.html');
			#$ti->swap['head'] .= "\t\t<title>".$ti->swap['title']."</title>\n";
			#$ti->swap['head'] .= "\t\t<base href=\"".$ti->config['template.base']."\" />\n";
			$this->handle_includes();
		}
		else if(file_exists($ti->paths['server'].'/'.$ti->config['template'].'/'.$ti->config['template.layout'].'.php'))
		{
			$this->type = 'php';
			include($ti->paths['server'].'/'.$ti->config['template'].'/'.$ti->config['template.layout'].'.php');
			$this->handle_includes();
			$this->html = $template->render();
		}
		else
		{
			throw new Exception('Template not found: '.$ti->config['template']);
		}
	}
	
	function handle_includes()
	{
		global $ti;
		
		if(file_exists($ti->paths['server'].'/'.$ti->config['template'].'/config.php'))
		{
			include($ti->paths['server'].'/'.$ti->config['template'].'/config.php');
		}
		
		foreach($ti->config['template.js'] as $js=>$do)
		{
			if($do)
			{
				if($this->type == 'html')
				{
					$ti->swap['head'] .= "\t\t".'<script language="Javascript" type="text/javascript" src="'.$js.'"></script>'."\n";
				}
				else if ($this->type == 'php')
				{
					$js = new ui_script($js);
					$ti->swap['head'] .= $js->render();
				}
			}
		}
		foreach($ti->config['template.css'] as $css=>$do)
		{
			if($do)
			{
				if($this->type == 'html')
				{
					$ti->swap['head'] .= "\t\t".'<link rel="StyleSheet" type="text/css" href="'.$css.'" />'."\n";
				}
				else if ($this->type == 'php')
				{
					$css = new ui_link('css',$css);
					$ti->swap['head'] .= $css->render();
				}
			}
		}
	}
	
	function render()
	{
		global $ti;
		$this->html = ti_util::swap($this->html,$ti->swap);
		echo($this->html);
	}
}
?>